<?php

namespace app\common\library;

/**
 * 韵达开放接口
 * @classname Yunda
 * @package app\common\library
 */
class Yunda
{
    public string $url     = 'http://gos-erp.yundasys.com:45120/v1/logistics/query/mn';
    public string $pwd     = '86eb5fee5cfc96abc32d4c1b4bdb601b';
    public string $account = 'nm20250612';
    /**
     * POST请求
     * @param {Object} $post_url
     * @param {Object} $post_data
     */
    public function curlPost($post_url, $post_data)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL, $post_url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        return curl_exec($ch);
    }

    /**
     * 发送请求
     ** @param $shipId
     * @return mixed
     *
     */
    public function send($shipId){
        $params['shipId']  = $shipId;
        $params['account'] = $this->account;
        $pwd               = $this->pwd;
        $params['sign']    = md5($params['account'].$pwd.$params['shipId']);
        $res =  $this->curlPost($this->url,$params);
        $data = json_decode($res,true);
//        if(isset($data['code']) && $data['code'] == 200){
//            usort($data['data'], function($a, $b) {
//                $timestampA = strtotime($a['time']);
//                $timestampB = strtotime($b['time']);
//                return $timestampB - $timestampA; // 倒序排列
//            });
//        }
        return $data;
    }
}
